<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="应用场景">
        <el-select v-model="queryParams.type">
          <el-option
            v-for="item in sceneOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoList" @selection-change="" border>
      <el-table-column label="序号" align="center" prop="orderNum" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            type="primary" plain
            v-show="scope.row.orderNum != 1"
            icon="el-icon-caret-top"
            size="small"
            @click="handleMoveUp(scope.$index, scope.row)"
          ></el-button>
          <el-button
            type="primary" plain
            v-show="scope.row.orderNum != total"
            icon="el-icon-caret-bottom"
            size="small"
            @click="handleMoveDown(scope.$index, scope.row)"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="播放" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-video-play"
            @click="handleVideo(scope.row)"
          >播放</el-button>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="name" />
      <el-table-column label="设备地址" align="center" prop="address" />
    </el-table>

    <!-- 添加或修改视频监控对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="900px" append-to-body>
      <el-form :model="unselected.queryParams" ref="unselectedVideoQueryForm" :inline="true" v-show="unselected.showSearch" label-width="68px">
        <el-form-item label="项目标段" prop="institutionCode">
          <el-select
            v-model="unselected.queryParams.institutionCode"
            placeholder="请选择"
            size="small"
            style="width: 240px" @change=""
          >
            <el-option-group
              v-for="group in unselected.projectBidOptions"
              :key="group.name"
              :label="group.name">
              <el-option
                v-for="item in group.bids"
                :key="item.code"
                :label="item.name"
                :value="item.code">
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入设备名称"
            clearable
            size="small"
            @keyup.enter.native="handleUnselectedVideoQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleUnselectedVideoQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetUnselectedVideoQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="unselected.loading" height="500px" :data="unselected.unselectedVideoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="标段名称" align="center" prop="institutionName" />
        <el-table-column label="设备名称" align="center" prop="name" />
        <el-table-column label="设备地址" align="center" prop="address" />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="unselected.queryParams.pageNum"
        :limit.sync="unselected.queryParams.pageSize"
        @pagination="getVideoUserUnselectedList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="unselected.multiple" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <videoPlayer ref="videoPlayer"></videoPlayer>

  </div>
</template>

<script>
  import { listVideoUser, listVideoUserUnselected, moveVideo, saveUserVideo } from "@/api/site/video/video";
  import { selectProjectBidBranch } from "@/api/project/bid";
  import videoPlayer from "@/views/site/video/video";

  export default {
    name: 'videoSetting',
    components: { videoPlayer },
    data() {
      return {
        // 遮罩层
        loading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 视频监控表格数据
        videoList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          type: null,
        },
        sceneOptions: [{value: 1, label: "智慧大屏"}],
        // 弹出层标题
        title: "选择视频监控",
        // 是否显示弹出层
        open: false,
        unselected: {
          // 查询参数
          queryParams: {
            pageNum: 1,
            pageSize: 10,
            institutionCode: null,
            name:null,
            type: null,
          },
          // 选中数组
          ids: [],
          // 非单个禁用
          single: true,
          // 非多个禁用
          multiple: true,
          // 总条数
          total: 0,
          // 未选择视频集合
          unselectedVideoList: [],
          // 遮罩层
          loading: false,
          // 显示搜索条件
          showSearch: true,
          // 标段下拉框
          projectBidOptions: [],
        }
      }
    },
    created() {
      this.queryParams.type = this.sceneOptions[0].value;
      this.unselected.queryParams.type = this.sceneOptions[0].value;
      this.getList();
      this.initBidSelect();
    },
    methods: {
      /** 查询视频监控列表 */
      getList() {
        this.loading = true;
        listVideoUser(this.queryParams).then(response => {
          this.videoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 初始化标段下拉框 */
      initBidSelect() {
        selectProjectBidBranch().then(response => {
          this.unselected.projectBidOptions = response.data;
        });
      },
      handleAdd() {
        this.unselected.unselectedVideoList = [];
        this.unselected.ids = [];
        this.getVideoUserUnselectedList();
        this.open = true;
        this.title = "选择视频监控";
      },
      getVideoUserUnselectedList() {
        this.unselected.loading = true;
        listVideoUserUnselected(this.unselected.queryParams).then(response => {
          this.unselected.unselectedVideoList = response.rows;
          this.unselected.total = response.total;
          this.unselected.loading = false;
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.unselected.ids = selection.map(item => item.id)
        this.unselected.single = selection.length!==1
        this.unselected.multiple = !selection.length
      },
      /** 搜索按钮操作 */
      handleUnselectedVideoQuery() {
        this.unselected.queryParams.pageNum = 1;
        this.getVideoUserUnselectedList();
      },
      /** 重置按钮操作 */
      resetUnselectedVideoQuery() {
        this.resetForm("unselectedVideoQueryForm");
        this.handleUnselectedVideoQuery();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 重置数据
      reset() {

      },
      submit() {
        this.saveUserVideo();
      },
      saveUserVideo() {
        let videoIds = '';
        this.unselected.ids.forEach((item) => {
          videoIds += item + ',';
        })
        videoIds = videoIds.substring(0, videoIds.length - 1);
        saveUserVideo({ type: this.unselected.queryParams.type, videoIds: videoIds}).then(response => {
          this.$modal.msgSuccess("保存成功");
          this.open = false;
          this.getList();
        })
      },
      // 上移
      handleMoveUp(index, row) {
        console.log("上移", index, row);
        const upData = this.videoList[index - 1];
        const downData = this.videoList[index];
        upData.orderNum = upData.orderNum + 1;
        downData.orderNum = downData.orderNum - 1;
        this.videoList.splice(index - 1, 1);
        this.videoList.splice(index, 0, upData);
        moveVideo({ id: row.id, type: "up"}).then(response => {

        });
      },
      // 下移
      handleMoveDown(index, row) {
        console.log("下移", index, row);
        const downData = this.videoList[index + 1];
        const upData = this.videoList[index];
        upData.orderNum = upData.orderNum + 1;
        downData.orderNum = downData.orderNum - 1;
        this.videoList.splice(index + 1, 1);
        this.videoList.splice(index, 0, downData);
        moveVideo({ id: row.id, type: "down"}).then(response => {

        });
      },
      handleVideo(row) {
        this.$refs.videoPlayer.show(row.videoId);
      },
    }
  }
</script>
